package com.promis.widget.mediapicker.ui;


import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.promis.util.CollectionUtils;
import com.promis.widget.R;
import com.promis.widget.mediapicker.entity.VideoParcelable;
import com.promis.widget.mediapicker.manager.MediaPickerConstants;
import com.promis.widget.mediapicker.util.FileUtils;
import com.promis.widget.mediapicker.util.ScreenUtils;

import java.util.List;

/**
 * Created by dmcBig on 2017/7/5.
 */

public class VideoGridAdapter extends RecyclerView.Adapter<VideoGridAdapter.MyViewHolder>{

    private List<VideoParcelable> videoList;
    private Context context;
    private int maxDuration, maxFileSize;
    public VideoGridAdapter(Context context, int maxDuration, int maxFileSize){
        this.maxDuration = maxDuration;
        this.maxFileSize = maxFileSize;
        this.context=context;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder{
        public ImageView media_image,check_image;
        public View mask_view;
        public TextView textView_time;
        public TextView textView_size;
        public LinearLayout video_info;
        public MyViewHolder(View view){
            super(view);
            media_image = (ImageView) view.findViewById(R.id.media_image);
            check_image = (ImageView) view.findViewById(R.id.check_image);
            mask_view =  view.findViewById(R.id.mask_view);
            video_info = (LinearLayout) view.findViewById(R.id.video_info);
            textView_size = (TextView) view.findViewById(R.id.textView_size);
            textView_time = (TextView) view.findViewById(R.id.textView_time);
            itemView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getItemWidth())); //让图片是个正方形

            media_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final VideoParcelable video = videoList.get(getLayoutPosition());
                    if (video.duration > ((maxDuration + 1) * 1000)) {
                        Toast.makeText(context,context.getString(R.string.msg_duration_limit) + maxDuration + "s",Toast.LENGTH_LONG).show();
                    } else if(video.size > maxFileSize){
                        Toast.makeText(context,context.getString(R.string.msg_size_limit)+(FileUtils.fileSize(maxFileSize)),Toast.LENGTH_LONG).show();
                    }else {
                        mOnItemClickListener.onItemClick(v, video);
                    }

                }
            });
        }
    }

    int getItemWidth(){
        return (ScreenUtils.getScreenWidth(context) / MediaPickerConstants.GridSpanCount)- MediaPickerConstants.GridSpanCount;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_mediapicker_grid_item,viewGroup,false);
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        if (CollectionUtils.isNullOrEmpty(videoList)) return;

        final VideoParcelable video = videoList.get(position);
        Uri mediaUri = Uri.parse("file://" + video.localPath);

        Glide.with(context)
                .load(mediaUri)
                .into(holder.media_image);

        holder.video_info.setVisibility(View.VISIBLE);
        holder.textView_size.setText("大小" + FileUtils.getSizeByUnit(video.size));
        if (video.duration <= 0) {
            video.duration = FileUtils.getDuration(video.localPath);
        }
        holder.textView_time.setText("视频" + FileUtils.getTimeByUnit(video.duration));

        holder.check_image.setVisibility(View.GONE);
    }

    public void notifyDataSetChanged(List<VideoParcelable> videoList){
        this.videoList = videoList;
        notifyDataSetChanged();
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        if (CollectionUtils.isNullOrEmpty(videoList)) return 0;
        else return videoList.size();
    }

    public  interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, VideoParcelable video);
    }
}
